<template>
  <div class="app-container">
<!--    <el-tabs style="margin: 10px;" type="border-card" @tab-click="handleClick">-->
<!--      <el-tab-pane label="问题列表">-->
        <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="问题题目" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入问题题目"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="问题答案" prop="answer">
            <el-input
              v-model="queryParams.answer"
              placeholder="请输入问题答案"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="点击量" prop="clickNum">
            <el-input
              v-model="queryParams.clickNum"
              placeholder="请输入点击量"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="问题类型" prop="typeId">
            <el-select v-model="queryParams.typeId" placeholder="请选择处理状态" clearable size="small">
              <el-option
                v-for="item in problemOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!--      <el-form-item label="申请人id" prop="applyId">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.applyId"-->
          <!--          placeholder="请输入申请人id"-->
          <!--          clearable-->
          <!--          size="small"-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
          <!--                  <el-form-item label="处理状态" prop="dealStatus">-->
          <!--                      <el-select v-model="queryParams.dealStatus" placeholder="请选择处理状态" clearable size="small">-->
          <!--                          <el-option-->
          <!--                                  v-for="item in problemOptions"-->
          <!--                                  :key="item.value"-->
          <!--                                  :label="item.label"-->
          <!--                                  :value="item.value"/>-->
          <!--                      </el-select>-->
          <!--                  </el-form-item>-->
          <el-form-item label="修改时间" prop="modifyTime">
            <el-date-picker
              v-model="queryParams.modifyTime"
              clearable
              size="small"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择修改时间"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['service:community:add']"
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['service:community:edit']"
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['service:community:remove']"
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['service:community:export']"
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >导出</el-button>
          </el-col>
            <el-col :span="1.5">
                <el-button
                        v-hasPermi="['service:community:add']"
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAddQuestionType"
                >新增问题类型</el-button>
            </el-col>
          <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
        </el-row>

        <el-table v-loading="loading" :data="communityList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="问题题目" align="center" prop="title" />
          <el-table-column label="问题答案" align="center" prop="answer"  show-overflow-tooltip/>
          <el-table-column label="点击量" align="center" prop="clickNum" width="100"/>
          <el-table-column label="问题类型" align="center" prop="type">
              <template slot-scope="scope">
                  {{scope.row.type}}
              </template>
          </el-table-column>
          <el-table-column label="修改时间" align="center" prop="modifyTime" width="150">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.modifyTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['service:community:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                v-hasPermi="['service:community:remove']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"/>
<!--      </el-tab-pane>-->
<!--      <el-tab-pane label="业主反馈问题">-->
<!--        <div style="margin:10px;">-->
<!--            <el-button-->
<!--                    v-hasPermi="['service:community:remove']"-->
<!--                    type="danger"-->
<!--                    plain-->
<!--                    icon="el-icon-delete"-->
<!--                    size="mini"-->
<!--                    :disabled="multiple"-->
<!--                    @click="handleDelete"-->
<!--            >删除</el-button>-->
<!--        </div>-->
<!--        <el-table-->
<!--          :data="questionApplyList"-->
<!--          style="width:100%;margin:10px;"-->
<!--          :default-sort="{prop: 'applyTime', order: 'descending'}">-->
<!--          <el-table-column-->
<!--            type="selection"-->
<!--            width="55"-->
<!--          />-->
<!--          <el-table-column-->
<!--            prop="answer"-->
<!--            label="内容"-->
<!--          />-->
<!--          <el-table-column-->
<!--            sortable-->
<!--            prop="createTime"-->
<!--            label="申请时间"-->
<!--            width="200px">-->
<!--              <template slot-scope="scope">-->
<!--                  {{parseTime(scope.row.createTime)}}-->
<!--              </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="nick_name"-->
<!--            label="申请人姓名"-->
<!--            width="120px"-->
<!--          />-->
<!--          <el-table-column-->
<!--            prop="phonenumber"-->
<!--            label="申请人联系方式"-->
<!--            width="120px"-->
<!--          />-->
<!--          <el-table-column-->
<!--            prop="dealStatus"-->
<!--            label="处理状态"-->
<!--            width="120px">-->
<!--              <template slot-scope="scope">-->
<!--                  {{scope.row.dealStatus == 0? '未处理' : '已完成'}}-->
<!--              </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            label="操作"-->
<!--            width="200px"-->
<!--          >-->
<!--            <template slot-scope="scope">-->
<!--              &lt;!&ndash;              <el-button type="text" size="small" @click="handleClick(scope.row)">查看详细</el-button>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-button&ndash;&gt;-->
<!--&lt;!&ndash;                        v-hasPermi="['service:community:edit']"&ndash;&gt;-->
<!--&lt;!&ndash;                        size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;                        type="text"&ndash;&gt;-->
<!--&lt;!&ndash;                        icon="el-icon-edit"&ndash;&gt;-->
<!--&lt;!&ndash;                        @click="showApplyDialog(scope.row)"&ndash;&gt;-->
<!--&lt;!&ndash;                >修改状态</el-button>&ndash;&gt;-->
<!--                <el-button-->
<!--                        v-hasPermi="['service:community:remove']"-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-delete"-->
<!--                        @click="handleDelete(scope.row)"-->
<!--                >删除</el-button>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--        </el-table>-->
<!--      </el-tab-pane>-->
<!--    </el-tabs>-->

    <!-- 添加或修改社区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题题目" prop="title">
          <el-input v-model="form.title" placeholder="请输入问题题目" />
        </el-form-item>
        <el-form-item label="问题答案" prop="answer">
          <el-input type="textarea" v-model="form.answer" placeholder="请输入问题答案" :rows="5"/>
        </el-form-item>
        <el-form-item label="问题类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择问题类型" clearable size="small">
            <el-option
              v-for="item in problemOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="点击量" prop="clickNum">-->
        <!--          <el-input v-model="form.clickNum" placeholder="请输入点击量" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="问题类型id" prop="typeId">-->
        <!--          <el-input v-model="form.typeId" placeholder="请输入问题类型id" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="申请人id" prop="applyId">-->
        <!--          <el-input v-model="form.applyId" placeholder="请输入申请人id" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="处理状态">-->
        <!--          <el-radio-group v-model="form.dealStatus">-->
        <!--            <el-radio label="1">请选择字典生成</el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="逻辑删除，0-删除 1-可用" prop="logicDelete">-->
        <!--          <el-input v-model="form.logicDelete" placeholder="请输入逻辑删除，0-删除 1-可用" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="修改时间" prop="modifyTime">-->
        <!--          <el-date-picker clearable size="small"-->
        <!--            v-model="form.modifyTime"-->
        <!--            type="date"-->
        <!--            value-format="yyyy-MM-dd"-->
        <!--            placeholder="选择修改时间">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

      <el-dialog title="新增问题类型" :visible.sync="questionTypeDialog">
          <el-form :model="questionType" label-width="120px">
              <el-form-item label="请输入问题类型">
                  <el-input v-model="questionType.type" autocomplete="off" />
              </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
              <el-button @click="cancelQuestionType">取 消</el-button>
              <el-button type="primary" @click="doAddQuestionType">新 增</el-button>
          </div>
      </el-dialog>

    <el-dialog :title="dialogTitle" :visible.sync="applyDialog">
      <el-form :model="applyProblemForm" label-width="80px">
        <el-form-item label="内容">
          <el-input v-model="applyProblemForm.answer" autocomplete="off" />
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select v-model="applyProblemForm.statusId" placeholder="请选择问题类型">
            <el-option
              v-for="item in applyProblemOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="applyDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommunity, getCommunity, delCommunity, addCommunity, updateCommunity, exportCommunity, getQuestionType, addQuestionType, initApplyList } from '@/api/service/questions'

export default {
  name: 'Community',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社区表格数据
      communityList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communityId: null,
        title: null,
        answer: null,
        clickNum: null,
        typeId: null,
        applyId: null,
        dealStatus: null,
        logicDelete: null,
        modifyTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      problemOptions: [],
      questionApplyList: [
        {
          content: '您好，我想了解物业费缴纳细节相关的问题',
          applyTime: '2021-06-26',
          person: {
            name: '张三',
            phone: '18812345678'
          },
          status: '未处理'
        },
        {
          content: '您好，我想了解物业停车位相关管理规则',
          applyTime: '2021-06-25',
          person: {
            name: '李四',
            phone: '18812345678'
          },
          status: '未处理'
        },
        {
          content: '您好，我想了解物业维修师傅的联系方式',
          applyTime: '2021-06-24',
          person: {
            name: '小明',
            phone: '18812345678'
          },
          status: '未处理'
        }
      ],
      applyDialog: false,
      applyProblemForm: {
        answer: '',
        statusId: ''
      },
      applyProblemOptions: [
        {
          value: 0,
          label: '未处理'
        }, {
          value: 1,
          label: '已完成'
        }
      ],
      questionTypeDialog: false,
      questionType: {}
    }
  },
  created() {
    this.initQuestionType()
    this.getList()
    this.getApplyList()
  },
  methods: {
    initQuestionType() {
      getQuestionType().then(resp => {
        if (resp) {
          console.log(resp)
          this.problemOptions = []
          resp.data.forEach(item => {
            this.problemOptions.push({
              value: item.id,
              label: item.type
            })
          })
        }
      })
    },
    /** 查询问题列表 */
    getList() {
      this.loading = true
      listCommunity(this.queryParams).then(response => {
        this.communityList = response.rows
        this.total = response.rows.length
        this.loading = false
      })
    },
    /** 查询申请列表 */
    getApplyList() {
      this.loading = true
      initApplyList(this.queryParams).then(response => {
        console.log(response.rows)
        this.questionApplyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        communityId: null,
        title: null,
        answer: null,
        clickNum: null,
        typeId: null,
        applyId: null,
        dealStatus: 0,
        logicDelete: null,
        createTime: null,
        modifyTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加社区'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCommunity(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改社区'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCommunity(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCommunity(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除【' + row.title + '】?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCommunity(ids)
      }).then(() => {
        this.getList()
        this.getApplyList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有社区数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportCommunity(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    showApplyDialog(data) {
      this.applyProblemForm.answer = data.answer
      this.applyProblemForm.statusId = data.dealStatus
      this.applyDialog = true
    },
    handleAddQuestionType() {
      this.questionTypeDialog = true
    },
    cancelQuestionType() {
      this.questionType = {}
      this.questionTypeDialog = false
    },
    doAddQuestionType() {
      addQuestionType(this.questionType).then(resp => {
        if (resp) {
          this.cancelQuestionType()
          this.initQuestionType()
          this.msgSuccess('新增成功')
        }
      })
    }
  }
}
</script>
